#!/bin/bash
# -*- coding: utf-8 -*-
# 新闻日报系统停止脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 打印横幅
print_banner() {
    echo "============================================================"
    echo "📰 MorningByte 新闻日报系统 - 停止服务"
    echo "============================================================"
    echo "停止时间: $(date '+%Y-%m-%d %H:%M:%S')"
    echo "============================================================"
}

# 停止Python进程
stop_python_processes() {
    print_info "停止Python进程..."
    
    # 查找并停止相关进程
    PIDS=$(pgrep -f "start_daily_news.py\|scheduler.py\|news_daily.py" 2>/dev/null || true)
    
    if [ -n "$PIDS" ]; then
        print_info "找到运行中的进程: $PIDS"
        echo "$PIDS" | xargs kill -TERM 2>/dev/null || true
        
        # 等待进程结束
        sleep 2
        
        # 检查是否还有进程在运行
        REMAINING_PIDS=$(pgrep -f "start_daily_news.py\|scheduler.py\|news_daily.py" 2>/dev/null || true)
        if [ -n "$REMAINING_PIDS" ]; then
            print_warning "强制停止剩余进程..."
            echo "$REMAINING_PIDS" | xargs kill -KILL 2>/dev/null || true
        fi
        
        print_success "Python进程已停止"
    else
        print_info "未找到运行中的Python进程"
    fi
}

# 停止系统服务
stop_system_service() {
    print_info "检查系统服务..."
    
    if command -v systemctl &> /dev/null; then
        if systemctl is-active --quiet morningbyte-news.service 2>/dev/null; then
            print_info "停止系统服务..."
            sudo systemctl stop morningbyte-news.service
            print_success "系统服务已停止"
        else
            print_info "系统服务未运行"
        fi
    fi
}

# 清理临时文件
cleanup_temp_files() {
    print_info "清理临时文件..."
    
    # 清理Python缓存
    find . -name "*.pyc" -delete 2>/dev/null || true
    find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true
    
    # 清理临时日志
    rm -f *.log 2>/dev/null || true
    
    print_success "临时文件清理完成"
}

# 显示服务状态
show_status() {
    print_info "检查服务状态..."
    
    # 检查Python进程
    PIDS=$(pgrep -f "start_daily_news.py\|scheduler.py\|news_daily.py" 2>/dev/null || true)
    if [ -n "$PIDS" ]; then
        print_warning "仍有Python进程在运行: $PIDS"
    else
        print_success "所有Python进程已停止"
    fi
    
    # 检查系统服务
    if command -v systemctl &> /dev/null; then
        if systemctl is-active --quiet morningbyte-news.service 2>/dev/null; then
            print_warning "系统服务仍在运行"
        else
            print_success "系统服务已停止"
        fi
    fi
}

# 主函数
main() {
    print_banner
    
    # 停止服务
    stop_python_processes
    stop_system_service
    
    # 清理临时文件
    cleanup_temp_files
    
    # 显示状态
    show_status
    
    print_success "服务停止完成"
    echo ""
    echo "💡 提示:"
    echo "- 要重新启动服务，请运行: ./start.sh"
    echo "- 要查看日志，请检查 logs/ 目录"
}

# 运行主函数
main "$@"
